package com.fenghongzhang.day009;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private Button create;

    private SQLiteDatabase readableDatabase;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        MySqlHelp mySqlHelp = new MySqlHelp(MainActivity.this,"one.db",null,2);
        readableDatabase = mySqlHelp.getReadableDatabase(); //open



        create.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

    }

    private void initView() {
        create = (Button) findViewById(R.id.create);

    }

    public void insert(View view) {
        //1 原生sql语句
        // ? 是占位符

        readableDatabase.beginTransaction();

        String sql ="insert into student values( null,?)";
        readableDatabase.execSQL(sql,new Object[]{"尅爱的琦琦2"});
        //2,api
        ContentValues contentValues = new ContentValues();
        contentValues.put("name","可爱的雨佳");
        readableDatabase.insert("student",null,contentValues);

        //提交事务
        readableDatabase.setTransactionSuccessful();
        //结束
        readableDatabase.endTransaction();

    }

    public void select(View view) {
        //1sql
//        readableDatabase.execSQL("select * from student");
        //游标
//        Cursor cursor = readableDatabase.rawQuery("select * from student", null);
//        String sql = "select * from student";
//        Cursor cursor = readableDatabase.rawQuery(sql,null);
//        if (cursor != null){
//            //有数据就移动指针,游标
//            while(cursor.moveToNext()){
//                String name = cursor.getString(cursor.getColumnIndex("name"));
//                String id = cursor.getString(cursor.getColumnIndex("id"));
//                Toast.makeText(this, id+name, Toast.LENGTH_SHORT).show();
//            }
//        }

        //2api
        //select * from student
        //select name from student where id=3;
//        Cursor cursor = readableDatabase.query("student", new String[]{"id","name"}, "id=?", new String[]{"3"}, null, null, null);
        Cursor cursor = readableDatabase.query("student", null, null, null, null, null, null);

        if (cursor != null){
            //有数据就移动指针,游标
            while(cursor.moveToNext()){
                String name = cursor.getString(cursor.getColumnIndex("name"));
                String id = cursor.getString(cursor.getColumnIndex("id"));
                Toast.makeText(this, id+name, Toast.LENGTH_SHORT).show();
            }
            cursor.close();
        }
    }

    public void del(View view) {
//        readableDatabase.execSQL("delete from student where id = ?" ,new String[]{"1"});
        readableDatabase.delete("student","id = ?",new String[]{"3"});
    }

    public void update(View view) {
//        String sql = "update student set name = '小小明' where id = ? and name =?";
//        readableDatabase.execSQL( sql,new String[]{"3","大明"});
        ContentValues contentValues = new ContentValues();
        contentValues.put("name","大大明");
        readableDatabase.update("student",contentValues,"id=?",new String[]{"3"});
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        readableDatabase.close();
    }
}
